package com.toaa.crazymath;

import java.util.Random;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.android.gms.games.Games;
import com.toaa.crazymath.Constant.GameDifficulty;
import com.toaa.crazymath.Constant.GameMode;
import com.toaa.crazymath.controller.BaseOperator;
import com.toaa.crazymath.controller.OperatorEntity;
import com.toaa.crazymath.controller.OperatorFactory;
import com.toaa.crazymath.utils.SoundManager;
import com.vn.toaa.brainracer.R;

public class PlayFragment extends Fragment implements OnClickListener {

	public static final String TAG = PlayFragment.class.getSimpleName();
	private SharedPreferences mPreferences;
	private int mGameDifficulty = GameDifficulty.NORMAL.ordinal();
	private int mGameMode = GameMode.ADDITION.ordinal();
	private boolean mIsVolumeOn = true;
	
	private TextView mTvExpression;
	private TextView mTvResult;
	private TextView mTvPoint;
	private Button mBtCorrect;
	private Button mBtWrong;
	private Button mBtVolume;
	private ProgressBar mPbTime;
	private Animation mAnimShake;
	private Animation mAnimInTop;
	private Animation mAnimOutTop;
	private Animation mAnimAlphaNewRecord;
	private View mRootView;
	private Dialog mDialog;
	private ImageView mIvSplash;
	
	private int mCurrentPoint = 0;
	private int mThinkingTime = Constant.THINKING_TIME_MAP.get(GameDifficulty.NORMAL.ordinal());
	private CountDownTimer mCountDownTimer;
	private BaseOperator mOperator;
	private Vibrator mVibrator;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPreferences = getActivity().getSharedPreferences(getActivity().getPackageName(), Context.MODE_PRIVATE);
		if (mPreferences.getBoolean(Constant.PREF_FIRST_PLAY, true)) {
			mPreferences.edit().putBoolean(Constant.PREF_FIRST_PLAY, false);
			final MainActivity mainActivity = (MainActivity) getActivity();
			unlockAchievement(mainActivity, getString(R.string.achievement_first_play));
		}
		mVibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
		mAnimShake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
		mAnimShake.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				if (mCountDownTimer != null) {
					mCountDownTimer.cancel();
				}
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				setEnableForAnswerLayout(true);
				displayResultDialog();
			}
		});
		mAnimInTop = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_in_down);
		mAnimInTop.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				if (mIsVolumeOn) {
					SoundManager.getInstance(getActivity()).playSoundReset();
				}
				mIvSplash.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				mIvSplash.startAnimation(mAnimOutTop);
			}
			
		});
		mAnimOutTop = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_out_up);
		mAnimOutTop.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				mCurrentPoint = 0;
				if (mPbTime != null) {
					mPbTime.setProgress(mPbTime.getMax());
				}
				setValueForViews();
				setBackgroundByRandomColor();
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				mIvSplash.setVisibility(View.GONE);
			}
		});
		mAnimAlphaNewRecord = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_alpha_new_record);
	}

	/**
	 * Unlock achievement
	 * 
	 * @param mainActivity
	 * @param achievementString
	 */
	private void unlockAchievement(final MainActivity mainActivity, final String achievementString) {
		if (mainActivity != null) {
			if (mainActivity.getApiClient().isConnected())
				Games.Achievements.unlock(mainActivity.getApiClient(), achievementString);
		}
	}
	
	/**
	 * 
	 * @param mainActivity
	 * @param leaderBoardID
	 * @param score
	 */
	public void submitScoreToLeaderboard(MainActivity mainActivity, final String leaderBoardID, long score) {
		if (mainActivity != null) {
			if (mainActivity.getApiClient().isConnected()) {
				Games.Leaderboards.submitScore(mainActivity.getApiClient(), leaderBoardID, score);
			}
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mCurrentPoint = 0;
		if (mPreferences != null) {
			mGameDifficulty = mPreferences.getInt(Constant.PREF_GAME_DIFFICULTY, GameDifficulty.NORMAL.ordinal());
			mGameMode = mPreferences.getInt(Constant.PREF_GAME_MODE, GameMode.ADDITION.ordinal());
			mIsVolumeOn = mPreferences.getBoolean(Constant.PREF_VOLUME_STATE, true);
		}
		
		mThinkingTime = Constant.THINKING_TIME_MAP.get(mGameDifficulty);
		
		mRootView = inflater.inflate(R.layout.play_fragment, null);
		mTvExpression = (TextView) mRootView.findViewById(R.id.textview_expression);
		mTvResult = (TextView) mRootView.findViewById(R.id.textview_result);
		mTvPoint = (TextView) mRootView.findViewById(R.id.textview_point);
		mBtCorrect = (Button) mRootView.findViewById(R.id.button_correct);
		mBtWrong = (Button) mRootView.findViewById(R.id.button_wrong);
		mBtVolume = (Button) mRootView.findViewById(R.id.button_volume);
		mIvSplash = (ImageView) mRootView.findViewById(R.id.imageview_splash);
		mPbTime = (ProgressBar) mRootView.findViewById(R.id.progressbar_time);
		final Button btVibrate = (Button) mRootView.findViewById(R.id.button_vibrate);

		mBtCorrect.setOnClickListener(this);
		mBtWrong.setOnClickListener(this);
		mBtVolume.setOnClickListener(this);
		btVibrate.setOnClickListener(this);
		
		Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "UVNThangVu.TTF");
		mTvExpression.setTypeface(typeface);
		mTvResult.setTypeface(typeface);
		typeface = Typeface.createFromAsset(getActivity().getAssets(), "UVNThangVu.TTF");
		mTvPoint.setTypeface(typeface);
		mBtVolume.setActivated(mIsVolumeOn);
		btVibrate.setActivated(mPreferences.getBoolean(Constant.PREF_VIBRATE_STATE, true));
		mOperator = OperatorFactory.getInstance().createOperator(mGameMode);
		mPbTime.setMax(mThinkingTime);
		mPbTime.setProgress(mPbTime.getMax());
		setValueForViews();
		setBackgroundByRandomColor();
		return mRootView;
	}
	
	/**
	 * 
	 */
	private void setValueForViews() {
		mTvPoint.setText(mCurrentPoint + "");
		final OperatorEntity operatorEntity = mOperator.createExpression();
		mTvExpression.setText(formatExpression(operatorEntity));
		mTvResult.setText(formatResult(operatorEntity));
	}
	
	private void startThinkingProgress() {
		mPbTime.setProgress(mPbTime.getMax());
		if (mCountDownTimer != null ) {
			mCountDownTimer.cancel();
		}
		mCountDownTimer = new CountDownTimer(mThinkingTime, 10) {
			
			@Override
			public void onTick(final long millisUntilFinished) {
				getActivity().runOnUiThread(new Runnable() {
					public void run() {
						mPbTime.setProgress((int) millisUntilFinished);
					}
				});
			}
			
			@Override
			public void onFinish() {
				getActivity().runOnUiThread(new Runnable() {
					public void run() {
						mPbTime.setProgress(0);
						if (mIsVolumeOn) {
							SoundManager.getInstance(getActivity()).playSoundWrong();
						}
						setEnableForAnswerLayout(false);
						mRootView.startAnimation(mAnimShake);
					}
				});
			}
		};
		mCountDownTimer.start();
	}

	private String formatExpression(OperatorEntity entity) {
		final int factor1 = entity.getFactor1();
		final int factor2 = entity.getFactor2();
		String operator = "+";
		if (mGameMode == GameMode.ADDITION.ordinal()) {
			operator = "+";
		} else if (mGameMode == GameMode.SUBTRACTION.ordinal()) {
			operator = "-";
		} else if (mGameMode == GameMode.MULTIPLICATION.ordinal()) {
			operator = "×";
		} else {
			operator = "÷";
		}
		return String.format("%d%s%d", factor1, operator, factor2);
	}

	private String formatResult(OperatorEntity entity) {
		return String.format("=%d", entity.getResult());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_correct:
			if (mOperator.checkExpression(mGameMode)) {
				Log.d(TAG, "isCorrect");
				if (mIsVolumeOn) {
					SoundManager.getInstance(getActivity()).playSoundCorrect();
				}
				mCurrentPoint ++;
				setValueForViews();
				startThinkingProgress();
			} else {
				Log.d(TAG, "wrong");
				if (mCountDownTimer != null) {
					mCountDownTimer.cancel();
				}
				if (mPreferences.getBoolean(Constant.PREF_VIBRATE_STATE, true)) {
					if (mVibrator != null) {
						mVibrator.vibrate(300);
					}
				}
				if (mIsVolumeOn) {
					SoundManager.getInstance(getActivity()).playSoundWrong();
				}
				setEnableForAnswerLayout(false);
				mRootView.startAnimation(mAnimShake);
			}
			break;
		case R.id.button_wrong:
			if (!mOperator.checkExpression(mGameMode)) {
				Log.d(TAG, "isCorrect");
				if (mIsVolumeOn) {
					SoundManager.getInstance(getActivity()).playSoundCorrect();
				}
				mCurrentPoint ++;
				setValueForViews();
				startThinkingProgress();
			} else {
				Log.d(TAG, "wrong");
				if (mCountDownTimer != null) {
					mCountDownTimer.cancel();
				}
				if (mPreferences.getBoolean(Constant.PREF_VIBRATE_STATE, true)) {
					if (mVibrator != null) {
						mVibrator.vibrate(300);
					}
				}
				if (mIsVolumeOn) {
					SoundManager.getInstance(getActivity()).playSoundWrong();
				}
				setEnableForAnswerLayout(false);
				mRootView.startAnimation(mAnimShake);
			}
			break;
		case R.id.button_play:
			if (isAdded()) {
				dismissDialog();
			}
			mIvSplash.setVisibility(View.VISIBLE);
			final ViewTreeObserver treeObserver = mIvSplash.getViewTreeObserver();
			if (treeObserver.isAlive()) {
				final OnPreDrawListener l = new OnPreDrawListener() {
					@Override
					public boolean onPreDraw() {
						treeObserver.removeOnPreDrawListener(this);
						mIvSplash.requestFocus();
						mIvSplash.post(new Runnable() {
							public void run() {
								mIvSplash.startAnimation(mAnimInTop);
							}
						});
						return false;
					}
				};
				treeObserver.addOnPreDrawListener(l);
			}
			break;
		case R.id.button_menu:
			dismissDialog();
			getActivity().getSupportFragmentManager().beginTransaction()
					.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
					.replace(R.id.framelayout_container, new MenuFragment()).commit();
			break;
		case R.id.button_volume:
			mIsVolumeOn = !mIsVolumeOn;
			mBtVolume.setActivated(mIsVolumeOn);
			mPreferences.edit().putBoolean(Constant.PREF_VOLUME_STATE, mIsVolumeOn).commit();
			break;
		case R.id.button_vibrate:
			final boolean newStateVibrate = !mPreferences.getBoolean(Constant.PREF_VIBRATE_STATE, true);
			v.setActivated(newStateVibrate);
			mPreferences.edit().putBoolean(Constant.PREF_VIBRATE_STATE, newStateVibrate).commit();
			break;
		default:
			break;
		}
	}
	
	private int getAdditionBestPoint() {
		int bestPoint = 0;
		if (mGameDifficulty == GameDifficulty.EASY.ordinal()) {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_ADDITION_EASY, 0);
		} else if (mGameDifficulty == GameDifficulty.NORMAL.ordinal()) {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_ADDITION_NORMAL, 0);
		} else {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_ADDTION_HARD, 0);
		}
		return bestPoint;
	}
	
	private int getSubtractionBestPoint() {
		int bestPoint = 0;
		if (mGameDifficulty == GameDifficulty.EASY.ordinal()) {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_SUBTRACTION_EASY, 0);
		} else if (mGameDifficulty == GameDifficulty.NORMAL.ordinal()) {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_SUBTRACTION_NORMAL, 0);
		} else {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_SUBTRACTION_HARD, 0);
		}
		return bestPoint;
	}
	
	private int getMultiBestPoint() {
		int bestPoint = 0;
		if (mGameDifficulty == GameDifficulty.EASY.ordinal()) {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_MULTI_EASY, 0);
		} else if (mGameDifficulty == GameDifficulty.NORMAL.ordinal()) {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_MULTI_NORMAL, 0);
		} else {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_MULTI_HARD, 0);
		}
		return bestPoint;
	}
	
	private int getDivisionBestPoint() {
		int bestPoint = 0;
		if (mGameDifficulty == GameDifficulty.EASY.ordinal()) {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_DIVISION_EASY, 0);
		} else if (mGameDifficulty == GameDifficulty.NORMAL.ordinal()) {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_DIVISION_NORMAL, 0);
		} else {
			bestPoint = mPreferences.getInt(Constant.PREF_BEST_POINT_DIVISION_HARD, 0);
		}
		return bestPoint;
	}
	
	private void setAdditionBesPoint() {
		final MainActivity mainActivity = (MainActivity) getActivity();
		if (mGameDifficulty == GameDifficulty.EASY.ordinal()) {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_ADDITION_EASY, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_easy_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_easy_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_easy_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_easy_5));
			}
		} else if (mGameDifficulty == GameDifficulty.NORMAL.ordinal()) {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_ADDITION_NORMAL, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_normal_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_normal_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_normal_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_normal_5));
			}
		} else {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_ADDTION_HARD, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_hard_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_hard_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_hard_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_addition_hard_5));
			}
		}
	}
	
	private void setSubtractionBestPoint() {
		final MainActivity mainActivity = (MainActivity) getActivity();
		if (mGameDifficulty == GameDifficulty.EASY.ordinal()) {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_SUBTRACTION_EASY, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_easy_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_easy_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_easy_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_easy_5));
			}
		} else if (mGameDifficulty == GameDifficulty.NORMAL.ordinal()) {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_SUBTRACTION_NORMAL, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_normal_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_normal_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_normal_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_normal_5));
			}
		} else {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_SUBTRACTION_HARD, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_hard_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_hard_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_hard_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_sub_hard_5));
			}
		}
	}
	
	private void setMultiBestPoint() {
		final MainActivity mainActivity = (MainActivity) getActivity();
		if (mGameDifficulty == GameDifficulty.EASY.ordinal()) {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_MULTI_EASY, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_easy_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_easy_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_easy_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_easy_5));
			}
		} else if (mGameDifficulty == GameDifficulty.NORMAL.ordinal()) {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_MULTI_NORMAL, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_normal_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_normal_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_normal_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_normal_5));
			}
		} else {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_MULTI_HARD, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_hard_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_hard_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_hard_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_multi_hard_5));
			}
		}
	}
	
	private void setDivisionBestPoint() {
		final MainActivity mainActivity = (MainActivity) getActivity();
		if (mGameDifficulty == GameDifficulty.EASY.ordinal()) {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_DIVISION_EASY, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_easy_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_easy_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_easy_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_easy_5));
			}
		} else if (mGameDifficulty == GameDifficulty.NORMAL.ordinal()) {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_DIVISION_NORMAL, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_normal_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_normal_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_normal_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_normal_5));
			}
		} else {
			mPreferences.edit().putInt(Constant.PREF_BEST_POINT_DIVISION_HARD, mCurrentPoint).commit();
			if (mCurrentPoint >= 50) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_hard_50));
			} else if (mCurrentPoint >= 20) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_hard_20));
			} else if (mCurrentPoint >= 10) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_hard_10));
			} else if (mCurrentPoint >= 5) {
				unlockAchievement(mainActivity, getString(R.string.achievement_div_hard_5));
			}
		}
	}
	
	private void displayResultDialog() {
		if (mDialog != null && mDialog.isShowing()) {
			return;
		}
		mDialog = new Dialog(getActivity(), R.style.DialogSlideAnim);
		mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		mDialog.setCancelable(false);
		final View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_result_notification, null);
		final Button btPlay = (Button) view.findViewById(R.id.button_play);
		final Button btMenu = (Button) view.findViewById(R.id.button_menu);
		final TextView tvCurrentPoint = (TextView) view.findViewById(R.id.textview_current_point);
		final TextView tvBestPoint = (TextView) view.findViewById(R.id.textview_best_point);
		final TextView tvTitle = (TextView) view.findViewById(R.id.textview_gameover_title);
		final TextView tvGameDifficult = (TextView) view.findViewById(R.id.textview_gameover_difficult);
		final TextView tvNameCurrent = (TextView) view.findViewById(R.id.textview_gameover_new);
		final TextView tvNameBest = (TextView) view.findViewById(R.id.textview_gameover_best);
		final ImageView ivNewRecord = (ImageView) view.findViewById(R.id.imageview_new_record);
		btPlay.setOnClickListener(this);
		btMenu.setOnClickListener(this);
		
		// get the best point from shared preference
		int bestPoint = 0;
		if (mGameMode == GameMode.ADDITION.ordinal()) {
			bestPoint = getAdditionBestPoint();
			tvTitle.setText("Addition Score");
		} else if (mGameMode == GameMode.SUBTRACTION.ordinal()) {
			bestPoint = getSubtractionBestPoint();
			tvTitle.setText("Subtration Score");
		} else if (mGameMode == GameMode.MULTIPLICATION.ordinal()) {
			bestPoint = getMultiBestPoint();
			tvTitle.setText("Multiplication Score");
		} else {
			bestPoint = getDivisionBestPoint();
			tvTitle.setText("Division Score");
		}
		
		if (mCurrentPoint > bestPoint) {
			// set the current best point into shared preference
			if (mGameMode == GameMode.ADDITION.ordinal()) {
				setAdditionBesPoint();
			} else if (mGameMode == GameMode.SUBTRACTION.ordinal()) {
				setSubtractionBestPoint();
			} else if (mGameMode == GameMode.MULTIPLICATION.ordinal()) {
				setMultiBestPoint();
			} else {
				setDivisionBestPoint();
			}
			ivNewRecord.setVisibility(View.VISIBLE);
			ivNewRecord.startAnimation(mAnimAlphaNewRecord);
		}
		if (mGameDifficulty == GameDifficulty.EASY.ordinal()) {
			tvGameDifficult.setText("Easy");
		} else if (mGameDifficulty == GameDifficulty.NORMAL.ordinal()) {
			tvGameDifficult.setText("Normal");
		} else {
			tvGameDifficult.setText("Hard");
		}
		
		final Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "UVNThangVu.TTF");
		tvTitle.setTypeface(typeface);
		tvGameDifficult.setTypeface(typeface);
		tvNameBest.setTypeface(typeface);
		tvNameCurrent.setTypeface(typeface);
		tvCurrentPoint.setTypeface(typeface);
		tvBestPoint.setTypeface(typeface);
		
		tvBestPoint.setText(bestPoint + "");
		tvCurrentPoint.setText(mCurrentPoint + "");
		
		WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
		final int screenWidth = windowManager.getDefaultDisplay().getWidth();
		final int defaultWidth = (int) (screenWidth * 0.9);
		mDialog.setContentView(view);
		mDialog.getWindow().setLayout(defaultWidth, LayoutParams.WRAP_CONTENT);
		if (mDialog != null && !mDialog.isShowing()) {
			mDialog.show();
		}
	}
	
	private void dismissDialog() {
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.cancel();
		}
	}
	
	private void setEnableForAnswerLayout(boolean isEnable) {
		mBtCorrect.setEnabled(isEnable);
		mBtWrong.setEnabled(isEnable);
	}
	
	public static class SplashDialog extends DialogFragment {
		
		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			Dialog dialog = new Dialog(getActivity(), R.style.DialogAnimationSplash);
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
			ImageView imageView = new ImageView(getActivity());
			imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			imageView.setImageResource(R.drawable.bg_menu_splash);
			imageView.setScaleType(ScaleType.FIT_XY);
			dialog.setContentView(imageView);
			return dialog;
		}
	}
	
	private void setBackgroundByRandomColor() {
		final int randomIndex = new Random().nextInt(Constant.COLORS.length);
		if (mRootView != null) {
			mRootView.setBackgroundColor(getResources().getColor(Constant.COLORS[randomIndex]));
		}
	}
}
